package com.lt.test.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.lt.test.api.HonestService;
import com.lt.test.common.exception.CustomException;
import com.lt.test.common.util.HttpClientUtil;
import com.lt.test.provider.mapper.HonestMapper;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;


@Service(version = "1.0.0")
public class HonestServiceImpl implements HonestService {

	private static final Logger logger = LoggerFactory.getLogger(HonestServiceImpl.class);
	@Resource
	private HonestMapper honestMapper;

	/**
	 * 更新异常信息
	 */
	@Override
	public void updateInfoException(Map<String, String> map) throws CustomException {
		honestMapper.updateInfoException(map);
	}

	/**
	 * 查看专卖异常总汇
	 */
	@Override
	public List<Map<String, String>> countException(Map<String, String> map) {
		List<Map<String, String>> list = honestMapper.countException(map);
		return list;
	}

	/**
	 * 查看异常处理明细
	 */
	@Override
	public Map<String, String> dealWithException(Map<String, String> map) {
		return honestMapper.dealWithException(map);
	}

	public Map<String, String> dateInfo(Map<String, String> map, String month) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		Date dt = new Date();
		String startdate = null;
		if ("1".equals(month)) {
			startdate = sdf.format(new Date());
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(dt); // 设置为当前时间
			calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
			dt = calendar.getTime();
			startdate = sdf.format(dt);
		}

		map.put("state_time", startdate);
		return map;
	}

	@Override
	public List<Map<String, String>> sales(Map<String, String> map, String month) {
		Map<String, String> dateInfo = dateInfo(map, month);
		List<Map<String, String>> map1 = honestMapper.sales(dateInfo);
		List<Map<String,String>> result = new ArrayList<Map<String,String>>();
		for (Map<String,String> m:map1 ) {
			for (String s:m.keySet()) {
				if("GROUP_ID".equals(s)||"USER_ID".equals(s)){
					continue;
				}
				Map<String,String>  map2 =new HashMap<String,String>();
				map2.put("CUST_NAME",s);
				map2.put("TOTAL",m.get(s));
				result.add(map2);
			}
		}
		return result;
	}

	@Override
	public List<Map<String, String>> aveprice(Map<String, String> map, String month) {
		Map<String, String> dateInfo = dateInfo(map, month);
		List<Map<String, String>> map1 = honestMapper.aveprice(dateInfo);
		List<Map<String,String>> result = new ArrayList<Map<String,String>>();
		for (Map<String,String> m:map1 ) {
			for (String s:m.keySet()) {
				if("GROUP_ID".equals(s)||"USER_ID".equals(s)){
					continue;
				}
				Map<String,String>  map2 =new HashMap<String,String>();
				map2.put("CUST_NAME",s);
				map2.put("TOTAL",m.get(s));
				result.add(map2);
			}
		}
		return result;
	}

	@Override
	public List<Map<String, String>> brandwidth(Map<String, String> map, String month) {
		Map<String, String> dateInfo = dateInfo(map, month);
		List<Map<String, String>> map1 = honestMapper.brandwidth(dateInfo);
		List<Map<String,String>> result = new ArrayList<Map<String,String>>();
		for (Map<String,String> m:map1 ) {
			for (String s:m.keySet()) {
				if("GROUP_ID".equals(s)||"USER_ID".equals(s)){
					continue;
				}
				Map<String,String>  map2 =new HashMap<String,String>();
				map2.put("CUST_NAME",s);
				map2.put("TOTAL",m.get(s));
				result.add(map2);
			}
		}
		return result;
	}

	@Override
	public List<Map<String, String>> topsales(Map<String, String> map, String month) {
		Map<String, String> dateInfo = dateInfo(map, month);
		return honestMapper.topsales(dateInfo);
	}

	@Override
	public List<Map<String, String>> showCustDept(Map<String, String> map) {
		return honestMapper.showCustDept(map);
	}

	@Override
	public List<Map<String, String>> salesSold(Map<String, String> map) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		String time = map.get("state_time");
		if (time == null) {
			String startdate = null;
			startdate = sdf.format(new Date());
			map.put("state_time", startdate);
		}
		return honestMapper.salesSold(map);
	}

	@Override
	public List<Map<String, String>> checkWidth(Map<String, String> map) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String time = map.get("state_time");
		if (time == null) {
			String startdate = null;
			startdate = sdf.format(new Date());
			map.put("state_time", startdate);
		} else {
			String state_time = time.substring(0, 4) + "-" + time.substring(4);
			map.put("state_time", state_time);
		}
		return honestMapper.checkWidth(map);
	}

	@Override
	public List<Map<String, String>> selectTop5(Map<String, String> map) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		String time = map.get("state_time");
		if (time == null) {
			String startdate = null;
			startdate = sdf.format(new Date());
			map.put("state_time", startdate);
		}
		return honestMapper.selectTop5(map);
	}

	@Override
	public void saveInfoException(Map<String, Object> map) throws CustomException {
		honestMapper.saveInfoException(map);

	}

	@Override
	public List<Map<String, String>> reportException(Map<String, String> map) {
		return honestMapper.reportException(map);
	}

	@Override
	public void saveRefundInfo(Map<String, String> map) throws CustomException {
		String id = map.get("id").toString();
		String refoundId = "TK" + id.substring(2);
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		map.put("refoundId", refoundId);
		map.put("operatorTime", now.toString());
		honestMapper.saveRefundInfo(map);
	}

	@Override
	public int checkIfPost(List<String> map) {
		return honestMapper.checkIfNeedPostage(map);
	}

	@Override
	public List<Map<String, String>> checkIfNeedPost(List<String> map) {
		return honestMapper.checkIfNeedPost(map);
	}

	@Override
	public List<Map<String, String>> queryGroupInfo(Map<String, String> map) {
		return honestMapper.queryGroupInfo(map);
	}

	@Override
	public Map<String, String> exchangeCdkey(Map<String, String> map)throws Exception{
		String userId = map.get("userId");
		Date dt = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = sdf.format(dt);
        map.put("dateString",dateString);
		Map<String, String> map1 = honestMapper.exchangeCdkey(map);
		Map<String,String> result = new HashMap<String,String>();
		String state = "";
		String amt = "";
		try{
			state=map1.get("STATE");
			amt=map1.get("AMT");
		}catch (NullPointerException e ){
			throw new CustomException("兑换码不存在");
		}
		if("1".equals(state)){
			throw new CustomException("兑换码已兑换奖品");
		}else{
			try{
				honestMapper.editCdkey(map);
				Map<String, String> data = new HashMap<String, String>();

				data.put("userId", userId);
				data.put("amt", amt);
				data.put("coinType", "3");
				data.put("url", "changecoinIntegral");
				data.put("type", "21");
				String url="http://10.95.0.233:23912/clouderp-sys-rest/api/coin/coinIntegral";
				String responseResult = HttpClientUtil.sendPostRequest(url, data, null, null);
				JSONObject jsonObject = JSONObject.fromObject(responseResult);
				String code=jsonObject.get("code").toString();
				if(!("000".equals(code))){
					throw new Exception();
				}
			}catch (Exception e){
				e.printStackTrace();
				throw new CustomException("兑换积分过程失败");
			}
		}
		return result;
	}

	@Override
	public List<Map<String, String>> getGroupYearsData(Map<String, String> parameter) {

		return honestMapper.getGroupYearsData(parameter);
	}


}
